package com.dream.service.user.web.processor.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.response.BaseListResponse;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.service.user.api.TelCodeService;
import com.dream.service.user.api.UserAddrService;
import com.dream.service.user.domain.model.UserAddrModel;
import com.dream.service.user.web.model.FieldSchema;
import com.dream.service.user.web.model.Param;
import com.dream.service.user.web.processor.RequestProcessor;
import com.dream.service.user.web.processor.exception.ParamException;
import com.dream.service.user.web.processor.param.ParamProcessor;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kangd on 2016/3/24.
 */
class TelCodeRequestProcessor implements RequestProcessor {

    TelCodeService telCodeService;

    public TelCodeService getTelCodeService() {
        return telCodeService;
    }

    public void setTelCodeService(TelCodeService telCodeService) {
        this.telCodeService = telCodeService;
    }

    @Override
    public JSONObject processor(HttpServletRequest req) {
        JSONObject result = new JSONObject();
        String cmd = req.getParameter(Param.CMD);
        try {
            if (cmd.equals("send")) {
                this.send(req, result);
            }else {
                AppResultBuilder.buildCmdErrorResult(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildResult(result,"",false, ErrorCode.UNKOWN);
        }
        return result;
    }


    public void send(HttpServletRequest req,JSONObject result){
        String tel = req.getParameter(FieldSchema.TELEPHONE);
        if(StringUtils.isEmpty(tel)){
            AppResultBuilder.buildParamErrorResult(result,"tel is null","");
            return;
        }
        BaseResponse<Boolean>  response = null;
        try {
            response = telCodeService.sendCode(tel);
            if (response.isSuccess()) {
                AppResultBuilder.buildResult(result,response.getResult(),true,ErrorCode.OK.getCode(),response.getError_msg(),"");
            }else{
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }

    }
}
